package com.neusoft.a132wxyorderingdish.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.a132wxyorderingdish.R;
import com.neusoft.a132wxyorderingdish.adapter.FoodListAdapter;
import com.neusoft.a132wxyorderingdish.beans.CollectResultBean;
import com.neusoft.a132wxyorderingdish.beans.FoodBean;
import com.neusoft.a132wxyorderingdish.beans.ResultBean;
import com.neusoft.a132wxyorderingdish.common.Constants;
import com.neusoft.a132wxyorderingdish.listener.RetrofitListener;
import com.neusoft.a132wxyorderingdish.model.ShopModel;

import java.util.List;

public class FoodListActivity extends BaseActivity implements RetrofitListener {

    private RecyclerView recyclerView;
    private TextView textView;
    private FoodListAdapter foodListAdapter;
    private ImageButton button_back,button_collect;
    private String result;



    @Override
    void initViews() {
        setLayout_file(R.layout.activity_foodlist);
        textView=findViewById(R.id.shop_name);
        button_back=findViewById(R.id.button_back);
        button_collect=findViewById(R.id.foodlist_collect);
        recyclerView=findViewById(R.id.food_list);
    }

    @Override
    void initEvents() {
        button_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FoodListActivity.this.finish();
            }
        });

        button_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int shop_id = getIntent().getIntExtra("shop_id",0);
                collect(String.valueOf(shop_id));
            }
        });

    }

    @Override
    void initData() {

        int shop_id = getIntent().getIntExtra("shop_id", 0);
        String shop_name=getIntent().getStringExtra("shop_name");//获取店铺名字
        getFoodList(String.valueOf(shop_id),shop_name);
        iscollected(String.valueOf(shop_id));
        //将对应的店铺名字获取，在TextView上显示
        textView.setText(shop_name);



    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayoutManager layoutManager = new LinearLayoutManager(FoodListActivity.this);
        //设置垂直布局
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag){
            case Constants.GETFOODBYSHOP:
                foodListAdapter=new FoodListAdapter(FoodListActivity.this,(List<FoodBean>)o);
                recyclerView.setAdapter(foodListAdapter);
                break;
            case Constants.ISCOLLECTED:
                result=((CollectResultBean)o).getCollected().toString();
                if(result.equals("0")){
                    button_collect.setImageResource(R.drawable.ic_favorite_border);
                }
                else {
                    button_collect.setImageResource(R.drawable.ic_favorite);
                }
                break;
            case Constants.USERCOLLECTSHOP:

                if(((ResultBean)o).getSuccess().equals("1")) {
                    if (result.equals("0")) {
                        button_collect.setImageResource(R.drawable.ic_favorite);
                        Toast.makeText(FoodListActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                        result = "1";
                    } else {
                        button_collect.setImageResource(R.drawable.ic_favorite_border);
                        Toast.makeText(FoodListActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                        result = "0";
                    }
                }
                else {
                    Toast.makeText(FoodListActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void onFail() {

    }

    private void getFoodList(String shop_id,String shop_name){
        ShopModel shopModel=new ShopModel();
        shopModel.getFoodByShop(shop_id,this);
        shopModel.getShopById(shop_name,this);

    }

    private void iscollected(String shop_food_id){
        ShopModel shopModel=new ShopModel();
        shopModel.isCollected(getUser_id(),shop_food_id,"0",this);
    }
    private void collect(String shop_id){
        ShopModel shopModel=new ShopModel();
        shopModel.userCollectShop(getUser_id(),shop_id,this);
    }

}

